package org.domus.sipnmove.useragent.view;

import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import org.domus.sipnmove.useragent.Call;
import org.domus.sipnmove.useragent.UserAgent;
import org.domus.sipnmove.useragent.UserAgentObserver;

/**
 * Represents a user agent
 */
@SuppressWarnings("serial")
public class UserAgentView extends JFrame implements UserAgentObserver, WindowListener {

	private UserAgent userAgent;
	private JTextField addressTextField;

	public UserAgentView(UserAgent userAgent) {
		this.userAgent = userAgent;

		init();
	}

	/**
	 * Initialize the view
	 */
	private void init() {

		this.setSize(280, 200);

		this.getContentPane().setLayout(new FlowLayout());

		addressTextField = new JTextField("sip:kilian@10.44.161.53:5070");
		addressTextField.setColumns(20);
		this.getContentPane().add(addressTextField);

		JButton callButton = new JButton("Call");
		callButton.addActionListener(new CallButtonActionListener(userAgent, this));
		this.getContentPane().add(callButton);

		JButton quitButton = new JButton("Quit");
		quitButton.addActionListener(new QuitButtonActionListener(userAgent));
		this.getContentPane().add(quitButton);

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowListener(this);

	}

	@Override
	public void callCreated(Call createdCall) {

		CallView newCallView = new CallView(createdCall);
		createdCall.addCallObserver(newCallView);
		newCallView.setVisible(true);

	}

	/**
	 * @return The address text value
	 */
	public String getAddressText() {
		return addressTextField.getText();
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		userAgent.quit();
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

}
